

import { Notification, Message, Loading } from 'element-ui'

import { Commit, Dispatch } from 'vuex';
import { get, defaultsDeep, cloneDeep } from 'lodash'

import { getProcessDatebase, getProcessDetail, getProcessIndex, deleteVersion, designProcess, editVersion, preData, creatVersion, auditProcess, getVersion } from '@/api/process'



export interface State {

}

const state: State = {
    productOnDesignVersion: {},
    productOnModifyVersion: {
        drawingNumber: "",
        materialQuality: "",
        productName: "",
        remark: "",
        versionId: "",
        versionName: "",
    },
    firstSearch: {
        drawingNumber: '',
        name: '',
        noticeNumber: '',
        state: '',
        type: '',
        page: 1,
        pageSize: 20,
    },
    firstData: [],   //设计审核列表
    versionIdList: [],  //工艺版本选择表
    processDetailList: [],   //工艺详情（该产品可以选择的工艺版本表）
    processList: [],  //技术评审列表
    secondData: [],  //产品通知单
    threeSearch: {
        dispatchNumber: '',
        drawingNumber: '',
        processName: '',
        state: '',
        page: 1,
        pageSize: 20
    },

    fourSearch: {
        drawingNumber: '',
        name: '',
        state: '',
        page: 1,
        pageSize: 20
    },
    //fourData:[],
    sixSearch: {
        drawingNumber: '',
        page: 1,
        pageSize: 20
    },
    sixData: [],  //工艺版本库列表
    versionTotalNum: 0,
    addId: '',  // 新增版本获取的versionId
    addProduct: {},  // 新增版本获取的产品信息
    addName: '', //新增版本的产品名称
    supplierList: [],//供应商列表
    equipmentList: [],//设备列表
    processNameList: [],//工序列表
    supplierOutsourceList: [],//外协列表
    totalNumberIndex: 0,
    designRowId: ''


};

const getters = {};

const mutations = {
    setDesignRowId(state: any, payload: any) {
        state.designRowId = payload
    },
    SET_PRODUCT_ON_DESIGN_VERSION(state: any, payload: any) {
        state.productOnDesignVersion = payload
    },
    SET_PRODUCT_ON_MODIFY_VERSION(state: any, payload: any) {
        state.productOnModifyVersion = payload
    },
    SET_FIRST_SEARCH(state: any, payload: any) {
        state.firstSearch = payload
    },
    SET_FIRST_DATA(state: any, payload: any) {
        state.firstData = payload
    },

    SET_VERSION_ID_LIST(state: any, payload: any) {
        state.versionIdList = payload
    },
    SET_PROCESS_DETAIL_LIST(state: any, payload: any) {
        state.processDetailList = payload
    },

    SET_PROCESS_LIST(state: any, payload: any) {
        state.processList = payload
    },
    SET_SIX_DATA(state: any, payload: any) {
        state.sixData = payload
    },

    SET_VERSION_TOTAL_NUM(state: any, payload: any) {
        state.versionTotalNum = payload
    },

    SET_ADD_ID(state: any, payload: any) {
        state.addId = payload
    },
    SET_ADD_PRODUCT(state: any, payload: any) {
        state.addProduct = payload
    },
    SET_ADD_NAME(state: any, payload: any) {
        state.addName = payload
    },
    SET_SUPPLOER_LIST(state: any, payload: any) {
        state.supplierList = payload
    },
    SET_OUTSUPPLOER_LIST(state: any, payload: any) {
        state.supplierOutsourceList = payload
    },
    SET_PROCESS_NAME_LIST(state: any, payload: any) {
        state.processNameList = payload
    },
    SET_EQUIPMENT_LIST(state: any, payload: any) {
        state.equipmentList = payload
    },
    setAddId(state: any, payload: any) {
        state.addId = payload
    },
    setTotalNumberIndex(state: any, payload: any) {
        state.totalNumberIndex = payload
    },


};

const actions = {


    //first表单数据更新
    COMMIT_TABLE_firstDATA({ commit, state }: any) {
        let params = state.firstSearch

        getProcessIndex(params).then((resp) => {

            let productList = get(resp, 'productList', [])

            commit('SET_FIRST_DATA', productList)
        })

    },


    /**
     * 03_01_新建工艺版本
     */
    async CREAT_VERSION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await creatVersion(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let data = get(resp, 'data', [])
            let versionId = get(data, 'versionId', '')
            let productName = get(data, 'productName', '')
            let drawingNumber = get(data, 'drawingNumber', '')
            let materialQuality = get(data, 'materialQuality', '')
            let addProduct = {
                productName: productName,
                drawingNumber: drawingNumber,
                materialQuality: materialQuality
            }
            commit('SET_ADD_ID', versionId)
            commit('SET_ADD_PRODUCT', addProduct)
            console.log(resp);
            return true
        }
    },








    /**
     * 03-09 进入首页 / 首页查询
     */
    async GET_PROCESS_INDEX({ state, commit, rootState, dispatch }: any, payload: any) {

        let resp = await getProcessIndex(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let productList = get(resp, 'data', [])
            let totalNumber = get(resp, 'total', 0)
            commit('SET_FIRST_DATA', productList)
            commit('setTotalNumberIndex', totalNumber)
            
            console.log(resp);
            return true
        }
    },








    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */




    //3-02获取产品工艺数据详情  /process/detail
    async GET_PROCESS_DETAIL(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await getProcessDetail(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {

            //重构以后把版本数组不给返回了，去整体数组找版本数据
            let versionIdList = get(resp, 'data', [])
            console.log(versionIdList)
            commit('SET_VERSION_ID_LIST', versionIdList)
            let processDetailList = get(resp, 'data', [])
            commit('SET_PROCESS_DETAIL_LIST', processDetailList)


            //console.log(resp);
            return true
        }



    },
    //03-03首页审核  /process/audit
    async AUDIT_PROCESS(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await auditProcess(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            // dispatch('COMMIT_TABLE_firstDATA')
            console.log(resp);
            return true
        }
    },

    //03_06_工艺工时设计  /process/design
    async DESIGN_PROCESS(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await designProcess(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //  dispatch('COMMIT_TABLE_firstDATA')

            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },
    //3-07产品工艺库查询  
    async GET_PROCESS_DATEBASE(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await getProcessDatebase(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let processVersionList = get(resp, 'data', [])
            let total = get(resp, 'total', 0)
            commit('SET_SIX_DATA', processVersionList)
            commit('SET_VERSION_TOTAL_NUM', total)
            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },
    //3-08工艺版本删除 /process/version/delete
    async DELETE_VERSION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await deleteVersion(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //   Message.success('请求成功!')
            //   dispatch('COMMIT_TABLE_firstDATA')
            console.log(resp);
            return true
        }
    },

    //03_10_修改工艺版本   2-14-b修改工艺版本   /process/version/update
    async updata_VERSION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await editVersion(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {

            return true
        }
    },

    //03_11_获取工艺版本（单）
    async GET_VERSION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await getVersion(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            let versionIdList = get(resp, 'data', Object)
            // console.log('dan',versionIdList)
            let arr = [{ id: 1 }]
            arr.unshift(versionIdList)
            arr.pop()
            console.log(arr)
            commit('SET_VERSION_ID_LIST', arr)
            commit('SET_PROCESS_DETAIL_LIST', arr)
            return true
        }
    },

    //03_12_获取预准备数据集  
    async PREDATA_VERSION(
        { state, commit, rootState, dispatch }: any,
        payload: any
    ) {

        let resp = await preData(payload).catch((err: any) => {
            Notification.error(err)
        })

        if (resp) {
            //获取供应商，设备，工序名 列表
            let data = get(resp, 'data', [])
            let equipmentlist = get(data, 'equipmentList', [])
            let processList = get(data, 'processList', [])
            let supplierPurchaseList = get(data, 'supplierPurchaseList', [])
            let supplierOutsourceList = get(data, 'supplierOutsourceList', [])
            commit('SET_EQUIPMENT_LIST', equipmentlist)
            commit('SET_PROCESS_NAME_LIST', processList)
            commit('SET_SUPPLOER_LIST', supplierPurchaseList)
            commit('SET_OUTSUPPLOER_LIST', supplierOutsourceList)



            //   Message.success('请求成功!')
            console.log(resp);
            return true
        }
    },



}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
};